﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Data;
using System.Reflection;

namespace GeneratorLib
{
    public class DBTypeMapConfig
    {
        private static string _currentPath;
        private DataSet _dBTypeMap;

        static DBTypeMapConfig()
        {
            FileInfo fi = new FileInfo(Assembly.GetExecutingAssembly().Location);

            _currentPath = fi.Directory.FullName; //  Environment.CurrentDirectory;        
        }

        private static DBTypeMapConfig instance;
        public static DBTypeMapConfig Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DBTypeMapConfig();
                }
                return instance;
            }
        }

        private DBTypeMapConfig()
        {
            InitDBTypeMap();
        }
        private void InitDBTypeMap()
        {
            _dBTypeMap = new DataSet();
            //_dBTypeMap.ReadXml(Path.Combine(_currentPath, "DatatypeMap.cfg"));
        }


        public void UnRegister(string providerName)
        {
            if (_dBTypeMap.Tables.Contains(providerName))
            {
                _dBTypeMap.Tables.Remove(providerName);
            }
        }
        public void Register(string providerName, string tableXMLFile)
        {
            if (!_dBTypeMap.Tables.Contains(providerName))
            {
                DataTable table = new DataTable(providerName);
                table.Columns.Add("key", typeof(string));
                table.Columns.Add("value", typeof(string));
                table.Columns.Add("DBType", typeof(string));

                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
                doc.Load(tableXMLFile);
                var tableNode = doc.ChildNodes[1].ChildNodes[0];
                for (int i = 0; i < tableNode.ChildNodes.Count; i++)
                {
                    var node = tableNode.ChildNodes[i];
                    if (node.NodeType != System.Xml.XmlNodeType.Element)
                    {
                        continue;
                    }
                    var row = table.NewRow();
                    row[0] = node.Attributes["key"].Value;
                    row[1] = node.Attributes["value"].Value;
                    row[2] = node.Attributes["key"].Value;
                    foreach (System.Xml.XmlAttribute attr in node.Attributes)
                    {
                        if (attr.Name.Equals("DBType"))
                        {
                            row[2] = attr.Value;// node.Attributes["DBType"].Value;
                            break;
                        }
                    }
                    table.Rows.Add(row);
                }

                _dBTypeMap.Tables.Add(table);

            }

            //using (var typeMap = new DataSet())
            //{
            //    typeMap.ReadXml(tableXMLFile);
            //    var tb = typeMap.Tables[0].Clone();
            //    tb.TableName = providerName;

            //    if (!_dBTypeMap.Tables.Contains(providerName))
            //    {
            //        _dBTypeMap.Tables.Add(tb);
            //        foreach (DataRow r in tb.Rows)
            //        {
            //            r["key"] = string.Format("{0}", r["key"]).ToLower();
            //        }
            //    }
            //}
        }

        public string CurrentPath
        {
            get
            {
                return _currentPath;
            }
        }

        public DataSet DBTypeMap
        {
            get
            {
                return _dBTypeMap;
            }
        }



    }
}
